package com.openbravo.pos.panels.link;

import java.awt.BorderLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import com.openbravo.bean.Location;
import com.openbravo.bean.Product;
import com.openbravo.bean.viewbean.ProductStoreDiaryView;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.pos.panels.QuickTimerFilter;
import com.openbravo.pos.panels.event.LocationChangedListener;
import com.openbravo.pos.panels.event.TimeCycleChangedEvent;
import com.openbravo.pos.panels.event.TimeCycleChangedListener;
import com.openbravo.pos.panels.link.model.ProductStoreDiaryVTableModel;

public class JProductStoreDiaryPanel extends AbstractProductLinkTablePanel<ProductStoreDiaryView> implements TimeCycleChangedListener,LocationChangedListener {
	 protected QuickTimerFilter timerFilter;
	 protected Object[] filter;
	private JLabel leb;
	 
	public JProductStoreDiaryPanel(I_DataLogicSales dlSales) {
		super(dlSales, new ProductStoreDiaryVTableModel());
		JPanel panel1=new JPanel();
		panel1.setLayout(new java.awt.BorderLayout());
	    leb=new JLabel();
		panel1.add(leb,BorderLayout.EAST);
		timerFilter=new QuickTimerFilter();
		timerFilter.setDefineSelected(QuickTimerFilter.TIMERCYCLETYPE_Week);
		timerFilter.addTimeCycleChangedListener(this);
		panel1.add(timerFilter,BorderLayout.CENTER);
		panel.add(panel1, BorderLayout.WEST);
	}

	@Override
	public void baseBeanChanged(Product o) {
		   initFilter();
	       filter[6]=QBFCompareEnum.COMP_EQUALS;
	       filter[7]=baseBeanID;
	       reQuery();
		
	}

	@Override
	public void timeCycleChanged(TimeCycleChangedEvent e) {
		initFilter();
		reQuery();
	}

	private void initFilter(){
		if(filter==null){
			filter=new Object[10];
			filter[0]=QBFCompareEnum.COMP_NONE;
			
			filter[2]=QBFCompareEnum.COMP_NONE;
			
			filter[4]=QBFCompareEnum.COMP_NONE;
			
			filter[6]=QBFCompareEnum.COMP_NONE;
			
			filter[8]=QBFCompareEnum.COMP_NONE;
		}
	}
	
	private void reQuery(){
		filter[0]=timerFilter.getStartDate()== null? QBFCompareEnum.COMP_NONE: QBFCompareEnum.COMP_GREATEROREQUALS;
		filter[1]=timerFilter.getStartDate();
		filter[2]= timerFilter.getEndDate() == null? QBFCompareEnum.COMP_NONE: QBFCompareEnum.COMP_LESS;
		filter[3]=timerFilter.getEndDate();
		try {
			tableModel.resert(dlSales.listByFilter(ProductStoreDiaryView.class, filter));
	} catch (BasicException e) {
		new MessageInf(e).show(this);
	}
	}

	@Override
	public void valueSelectChanged(Location obj) {
		 initFilter();
		 filter[6]=QBFCompareEnum.COMP_EQUALS;
	     filter[7]=obj.getID();
	     leb.setText(obj.getName()+"-"+obj.getAddress());
		
	}
	
}
